﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.CRM.Library;
using ZX.BLL.Extend;
using ZX.CRM.Bussiness;
using System.Text;
using ZX.Common.Extend;
using System.Threading.Tasks;
using System.Data;
using ZX.EHR.Library;
using ZX.Web.UI.Controls;
using ZX.BLL;
namespace ZX.WebApp.Crm.Hodometer
{
    public partial class InCalendarSum : ListPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string strView = this.GetStringFromQueryString("view", "");
                if (strView == "check")
                {
                    rptComment.Visible = true;
                    lblArrive.Disabled = true;
                    btnPM.Visible = false;
                    txtAppraiseoneself.Enabled = false;
                    rdbxb.Enabled = false;
                    txtTrainBackground.Enabled = false;
                    txtPersonSum.Enabled = false;
                    rdbzw.Enabled = false;
                    rdbwh.Enabled = false;
                    taSubject1.Disabled = true;
                    taTContent1.Disabled = true;
                    taVideo1.Disabled = true;
                    taTCase1.Disabled = true;
                    txtFrontCommunicate.Enabled = false;
                    txtExcellence.Enabled = false;
                    txtBlemish.Enabled = false;
                    txtStudyGain.Enabled = false;
                    txtAssistantCommunicate.Enabled = false;
                    txtAssistantEasy.Enabled = false;
                    txtAssistantRemove.Enabled = false;
                    txtAssistantSound.Enabled = false;
                    txtAssistantMeetingPlace.Enabled = false;
                    txtAssistantFlow.Enabled = false;
                    btnSave.Visible = false;
                    txtExplain.Enabled = false;
                }
                string hodid = Request.QueryString["HDID"];
                banghod(hodid);
                bind();
            }
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        /// <param name="hodid"></param>
        public void banghod(string hodid)
        {
            string teaname = Request.QueryString["TNAME"];
            LabTeachName.Text = teaname;
            CrmInnerTeachSumup cits = new CrmInnerTeachSumup();
            CrmInnerTeachHodometer cith = new CrmInnerTeachHodometer();

            BaseBussnies bcits = new BaseBussnies(cits);
            BaseBussnies bcith = new BaseBussnies(cith);
            IList<ZX.CRM.Library.CrmInnerTeachSumup> citsl = bcits.GetEntitys<ZX.CRM.Library.CrmInnerTeachSumup>(" HodometerId='" + hodid + "'");
            IList<ZX.CRM.Library.CrmInnerTeachHodometer> cithl = bcith.GetEntitys<ZX.CRM.Library.CrmInnerTeachHodometer>(" ID='" + hodid + "'");

            if (cithl != null)//讲师行程
            {
                LabSEdate.Text = cithl[0].BeginDate.ToString("yyyy-MM-dd") + "--" + cithl[0].EndDate.ToString("yyyy-MM-dd");
                hdfbegindate.Value = cithl[0].BeginDate.ToString("yyyy-MM-dd");
            }

            if (citsl != null)//总结
            {
                ViewState["TS"] = citsl[0];
                SetTextValue(citsl[0]);
                hdfid.Value = citsl[0].ID.ToString();
                CrmInnerTeachContent citc = new CrmInnerTeachContent();
                BaseBussnies bcitc = new BaseBussnies(citc);
                IList<ZX.CRM.Library.CrmInnerTeachContent> citcl = bcitc.GetEntitys<ZX.CRM.Library.CrmInnerTeachContent>(" InnerSumID='" + citsl[0].ID + "' order by [DATE]");
                //
                StringBuilder sb = new StringBuilder();
                if (citcl != null)
                {
                    for (int i = 0; i < citcl.Count; i++)
                    {
                        sb.Append((i + 1).ToString() + "`" + citcl[i].Subject + "`" + citcl[i].Content + "`" + citcl[i].Video + "`" + citcl[i].Case + "︾");

                    }
                    hdfkcsj.Value = sb.ToString().Substring(0, sb.ToString().Length - 1);
                }
            }

        }
        /// <summary>
        /// 发表评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnComment_OnClick(object sender, EventArgs e)
        {
            DatUser du = new DatUser();
            du = Session["User"] as DatUser;
            CrmInnerComment cco = new CrmInnerComment();
            cco.TeachID = du.UserID;
            cco.ComContent = txtComment.Text;
            cco.InnerSumID = hdfid.Value == "" ? Guid.Empty : new Guid(hdfid.Value);
            cco.AddTime = DateTime.Now;
            BaseBussnies bli = new BaseBussnies();
            bli = new BaseBussnies(cco);
            bli.Insert();
            ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "", "javascript:window.location.reload();", true);
        }
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            bool bl = false;
            string kcsj = hdfkcsj.Value;
            string begindate = hdfbegindate.Value;
            if (hdfid.Value != null && hdfid.Value != "")
            {
                CrmInnerTeachSumup cits = new CrmInnerTeachSumup();
                if (ViewState["TS"] != null)
                {
                    cits = (ZX.CRM.Library.CrmInnerTeachSumup)ViewState["TS"];
                }
                else
                {
                    BaseBussnies bcits = new BaseBussnies(cits);
                    IList<ZX.CRM.Library.CrmInnerTeachSumup> citsl = bcits.GetEntitys<ZX.CRM.Library.CrmInnerTeachSumup>(" HodometerId='" + hdfid.Value + "'");
                    if (citsl != null)
                        cits = citsl[0];
                }
                //cits.Adduser = (Guid)this.UserId;
                //cits.Addtime = DateTime.Now;
                cits = Gettxtvalue(cits);
                BaseBussnies bc = new BaseBussnies(cits);

                bl = bc.Update();
                inserupdateSumCont("1", cits.ID.ToString(), begindate, kcsj);
            }
            else
            {
                Guid id = Guid.NewGuid();
                CrmInnerTeachSumup cits = Gettxtvalue(new CrmInnerTeachSumup());
                cits.ID = id;
                try
                {
                    cits.HodometerId = new Guid(Request.QueryString["HDID"].ToString());
                    cits.Addtime = DateTime.Now;
                    cits.Adduser = (Guid)this.UserId;
                    BaseBussnies bc = new BaseBussnies(cits);
                    bl = bc.Insert();
                    hdfid.Value = id.ToString();
                }
                catch (Exception) { }
                inserupdateSumCont("0", cits.ID.ToString(), begindate, kcsj);
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertResult", "AlertResult('" + bl + "')", true);
        }
        /// <summary>
        /// 新增总结
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pid"></param>
        /// <param name="bd"></param>
        /// <param name="val"></param>
        private void inserupdateSumCont(string type, string pid, string bd, string val)
        {
            if (bd == "")
                return;
            DateTime db = Convert.ToDateTime(bd);
            string[] vals = val.Split('︾');
            if (type != "0")//更新的,先删除
            {
                BTCrmTeachHodometer bth = new BTCrmTeachHodometer();
                bth.deleteCrmInnerTeachContent(pid);
            }
            for (int i = 0; i < vals.Length; i++)
            {
                string[] vs = vals[i].Split('`');
                CrmInnerTeachContent citc = new CrmInnerTeachContent();
                citc.ID = Guid.NewGuid();
                citc.InnerSumID = new Guid(pid);
                citc.Date = db.AddDays(Convert.ToInt32(vs[0]) - 1);
                citc.Subject = vs[1];
                citc.Content = vs[2];
                citc.Video = vs[3];
                citc.Case = vs[4];
                BaseBussnies bc = new BaseBussnies(citc);
                bc.Insert();
            }

        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="cits"></param>
        /// <returns></returns>
        protected CrmInnerTeachSumup Gettxtvalue(CrmInnerTeachSumup cits)
        {
            cits.Appraiseoneself = txtAppraiseoneself.Text;
            cits.AssistantUser = tmpUser.Value;
            cits.EXP1 = lblArrive.Value;//讲师到达企业日期
            cits.TrainBackground = txtTrainBackground.Text;
            cits.PersonSum = txtPersonSum.Text == "" ? 0 : Convert.ToInt32(txtPersonSum.Text);
            cits.CultureDegree = new Guid(rdbwh.SelectedValue);
            cits.SexFrame = new Guid(rdbxb.SelectedValue);
            cits.PostFrame = new Guid(rdbzw.SelectedValue);
            cits.FrontCommunicate = txtFrontCommunicate.Text;
            cits.Excellence = txtExcellence.Text;
            cits.Blemish = txtBlemish.Text;
            cits.StudyGain = txtStudyGain.Text;
            cits.AssistantCommunicate = txtAssistantCommunicate.Text;
            cits.AssistantEasy = txtAssistantEasy.Text;
            cits.AssistantRemove = txtAssistantRemove.Text;
            cits.AssistantSound = txtAssistantSound.Text;
            cits.AssistantMeetingPlace = txtAssistantMeetingPlace.Text;
            cits.AssistantFlow = txtAssistantFlow.Text;
            cits.EXP4 = txtExplain.Text;
            return cits;
        }
        /// <summary>
        /// 赋值
        /// </summary>
        /// <param name="cits"></param>
        protected void SetTextValue(CrmInnerTeachSumup cits)
        {
            txtAppraiseoneself.Text = cits.Appraiseoneself;
            txtPM.Value = cits.AssistantUser;
            lblArrive.Value = cits.EXP1;//讲师到达企业日期
            txtTrainBackground.Text = cits.TrainBackground;
            txtPersonSum.Text = cits.PersonSum.ToString();
            rdbwh.SelectedValue = cits.CultureDegree.ToString();
            rdbxb.SelectedValue = cits.SexFrame.ToString();
            rdbzw.SelectedValue = cits.PostFrame.ToString();
            txtFrontCommunicate.Text = cits.FrontCommunicate;
            txtExcellence.Text = cits.Excellence;
            txtBlemish.Text = cits.Blemish;
            txtStudyGain.Text = cits.StudyGain;
            txtAssistantCommunicate.Text = cits.AssistantCommunicate;
            txtAssistantEasy.Text = cits.AssistantEasy;
            txtAssistantRemove.Text = cits.AssistantRemove;
            txtAssistantSound.Text = cits.AssistantSound;
            txtAssistantMeetingPlace.Text = cits.AssistantMeetingPlace;
            txtAssistantFlow.Text = cits.AssistantFlow;
            txtExplain.Text = cits.EXP4;
        }
        /// <summary>
        /// 绑定评论列表
        /// </summary>
        protected void bind()
        {
            BCrmTeachSumup bts = new BCrmTeachSumup();
            rptComment.DataSource = bts.GetCommentDetail(hdfid.Value == "" ? Guid.Empty : new Guid(hdfid.Value));
            rptComment.DataBind();
        }
        /// <summary>
        /// 获取评论讲师名称
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string getTeacher(object obj)
        {
            BUser buser = new BUser();
            DatUser user = new DatUser();
            user = buser.GetDatUserByUserId(obj == null ? Guid.Empty : new Guid(obj.ToString()));
            return user == null ? "" : user.UName;
        }
        /// <summary>
        /// 格式化时间
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string getDateTime(object obj)
        {
            return obj == null ? "" : Convert.ToDateTime(obj).ToString("yyyy-MM-dd HH:mm");
        }
        /// <summary>
        /// 是否是查看状态 值为check代表是
        /// </summary>
        public string getView
        { get { return this.GetStringFromQueryString("view", ""); } }
        /// <summary>
        /// 获取评论条数
        /// </summary>
        public string getNum
        { get { return this.rptComment.Items.Count.ToString(); } }
    }
}